<template>
  <div class="content">
    <div class="name">{{ item.name }}</div>
    <div class="manager">{{ item.manager }}</div>
    <el-dropdown @command="commandEvent">
      <div class="set">
        <span>操作</span>
        <i class="el-icon-arrow-down el-icon--right" />
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!isTop"
            command="look"
          >查看子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!isTop"
            command="del"
          >删除子部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDel } from '@/api/departments'
export default {
  props: {
    // 父组件传值,数据渲染
    item: {
      type: Object,
      default: () => ({})
    },
    isTop: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isShow: true
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          // 触发兄弟组件
          this.$bus.$emit('addEvent', true, this.item, 'add')
          break
        case 'look':
          this.$bus.$emit('addEvent', true, this.item, 'edit')
          break
        case 'del':
          // 删除确认框
          this.$confirm(`您确定要删除[${this.item.name}]部门吗?`, '温馨提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          }).then(async() => {
            // 调用删除部门接口
            await companyDepartmentDel(this.item)
            this.$message.success('删除部门成功!')
            // 调用父组件更新列表方法
            this.$emit('getDep')
          })
          break
      }
    }
  }
}
</script>

<style lang="scss" scpoed>
.content {
  display: flex;
  cursor: pointer;
  .name {
    flex: 1;
  }
  .manager {
    width: 90px;
  }
}
</style>
